import { QuestionCollection } from "inquirer";
import {
  groups,
  comps,
  plugins,
  groupData,
  demos,
  pluginGroups,
  pluginDemos,
  pluginGroupData,
} from "./data";

export const compNames = Object.keys(comps);
export const compGroupNames = Object.keys(groupData);
export const compDemoNames = Object.keys(demos);
export const pluginNames = Object.keys(plugins);
export const plugGroupNames = Object.keys(pluginGroupData)
export const plugDemoNames = Object.keys(pluginDemos)
//
export const dictComps = compNames.map(k => ({value:k,name:`${comps[k].label} [${k}]`}))
export const dictCompGroups = compGroupNames.map((k) => ({
  value: k,
  name: `${groupData[k].label} [${k}]`,
}));
export const dictCompDemos = compDemoNames.map(k => ({value:k,name:`${demos[k].title || demos[k].label} [${k}]`}))
export const dictPlugins = pluginNames.map((k) => ({ value: k, name: `${plugins[k].label} [${k}]` }));
export const dictPlugGroups = plugGroupNames.map((k) => ({
  value: k,
  name: `${pluginGroupData[k].label} [${k}]`,
}));
export const dictPlugDemos = plugDemoNames.map(k => ({value:k,name:`${pluginDemos[k].title || pluginDemos[k].label} [${k}]`}))
//
export const QS =  {
  D: {
    label: {
      type: "input",
      name: "label",
      message: "请输入标签名称",
    },
    desc: {
      type: "input",
      name: "desc",
      message: "请输入描述信息",
    },
    menu: {
      type: "input",
      name: "menu",
      message: "请输入组件菜单名称 默认使用 标签名称",
    },
    sort:{
      type: "number",
      name: "sort",
      message: "请填写排序序号，默认按照升序规则排序",
    },
    title:{
      type: "input",
      name: "menu",
      message: "请输入标题名称",
    },
    isDelete: {
      type: "confirm",
      name: "isDelete",
      message: "确认是否删除？",
      default: false,
    },
    isRebuild:{
      type: "confirm",
      name: "isRebuild",
      message: "确认是否重建文件？（会覆盖掉所有原始文件）",
      default: false,
    },
    isAddDemo:{
      type: "confirm",
      name: "isAddDemo",
      message: "是否新建 DEMO ？",
      default: false,
    },
    isReset:{
      type: "confirm",
      name: "isReset",
      message: "是否重置数据？",
      default: false,
    },
    isRefresh:{
      type: "confirm",
      name: "isRefresh",
      message: "是否执行刷新操作？",
      default: false,
    },
    isClear:{
      type: "confirm",
      name: "isClear",
      message: "是否清除所有数据？",
      default: false,
    }
  },
  compGroup: {
    parent: {
      type: "rawlist",
      name: "parent",
      message: "请选择父节点",
      choices: [{ name: "无", value: "root" }, ...dictCompGroups],
    },
    name: {
      type: "input",
      name: "name",
      message: "请输入分组唯一约束键名称",
      validate: (v) => {
        if (!compGroupNames.includes(v)) {
          return true;
        }
        return "键名称重复，请修改！";
      },
    },
    del:{
      type: "rawlist",
      name: "name",
      message: "请选择要删除的分组？(只能删除没有子项的分组！)",
      choices: dictCompGroups.filter((d) => groups[d.value]?.length == 0),
    },
    select:{
      type: "rawlist",
      name: "name",
      message:"请选择组件分组",
      choices: dictCompGroups
    }
  },
  comp: {
    select:{
      type:"rawlist",
      name:"name",
      message:"请选择组件",
      choices:dictComps
    },
    name: {
      type: "input",
      name: "name",
      message: "请输入组件名称(纯英文字符，建议用首字符大写的驼峰形式)",
      validate: (v) => {
        if (compNames.includes(v)) {
          return "键名称重复，请修改！";
        }
        if (!/^\w+$/.test(v)) {
          return "名称不符合规范！";
        }
        return true;
      },
    },
    del:{
      type:"rawlist",
      name:"name",
      message:"请选择要删除的组件",
      choices:dictComps
    },
    group:{
      type:"rawlist",
      name:"groupName",
      message:"请选择组件分组",
      choices:[{name:'新建分组',value:"new"},...dictCompGroups]
    }
  },
  compDemo: {
    name:{
      type:"input",
      name:"name",
      message:"请输入 DEMO 键名,请使用英文字符",
      default:`DEMO-${compDemoNames.length + 1}`,
      validate:(v) => {
        if(compDemoNames.includes(v)){
          return "键名称重复，请修改！"
        }
        if(!/^\w+$/.test(v)){
          return "名称不符合规范！"
        }
        return true;
      }
    },
    del:{
      type:"rawlist",
      name:"name",
      message:"请选择要删除的 DEMO",
      choices:dictCompDemos
    },
    select:{
      type:"rawlist",
      name:"name",
      message:"请选择 DEMO",
      choices:dictCompDemos
    },
  },
  plugin: {
    name: {
      type: "input",
      name: "name",
      message: "请输入插件名称(纯英文字符，建议用首字符大写的驼峰形式)",
      validate: (v) => {
        if (pluginNames.includes(v)) {
          return "键名称重复，请修改！";
        }
        if (!/^\w+$/.test(v)) {
          return "名称不符合规范！";
        }
        return true;
      },
    },
    del:{
      type:"rawlist",
      name:"name",
      message:"请选择要删除的插件",
      choices:dictPlugins
    },
    select:{
      type:"rawlist",
      name:"name",
      message:"请选择插件",
      choices:dictPlugins
    },
    group:{
      type:"rawlist",
      name:"groupName",
      message:"请选择插件分组",
      choices:[{name:'新建分组',value:"new"},...dictPlugGroups]
    }
  },
  pluginGroup:{
    parent: {
      type: "rawlist",
      name: "parent",
      message: "请选择父节点",
      choices: [{ name: "无", value: "root" }, ...dictPlugGroups],
    },
    name: {
      type: "input",
      name: "name",
      message: "请输入分组唯一约束键名称",
      validate: (v) => {
        if (!plugGroupNames.includes(v)) {
          return true;
        }
        return "键名称重复，请修改！";
      },
    },
    del:{
      type: "rawlist",
      name: "name",
      message: "请选择要删除的分组？(只能删除没有子项的分组！)",
      choices: dictPlugGroups.filter((d) => pluginGroups[d.value].length == 0),
    },
    select:{
      type:"rawlist",
      name:"name",
      message:"请选择插件分组",
      choices:dictPlugGroups
    },
    
  },
  pluginDemo:{
    name:{
      type:"input",
      name:"name",
      message:"请输入 DEMO 键名,请使用英文字符",
      default:`DEMO-${plugDemoNames.length + 1}`,
      validate:(v) => {
        if(plugDemoNames.includes(v)){
          return "键名称重复，请修改！"
        }
        if(!/^\w+$/.test(v)){
          return "名称不符合规范！"
        }
        return true;
      }
    },
    del:{
      type:"rawlist",
      name:"name",
      message:"请选择要删除的 DEMO",
      choices:dictPlugDemos
    },
    select:{
      type:"rawlist",
      name:"name",
      message:"请选择 DEMO",
      choices:dictPlugDemos
    },
  }
};

export const getPluginSortInfo = (groupName,isGroup) => {
  let arr = pluginGroups[groupName] || []
  arr = arr.filter(n => (isGroup ? plugGroupNames : pluginNames).includes(n))
  if(arr.length > 0){
    return {message:"请填写排序序号 当前排序：" + JSON.stringify(arr),default:arr.length}
  }else{
    return {message:"请填写排序序号 当前排序：[]",default:0}
  }
}

export const getCompSortInfo = (groupName,isGroup) => {
  let arr = groups[groupName] || [];
  arr = arr.filter(n => (isGroup ? compGroupNames : compNames).includes(n));
  if(arr.length > 0){
    return {message:"请填写排序序号 当前排序：" + JSON.stringify(arr),default:arr.length}
  }else{
    return {message:"请填写排序序号 当前排序：[]",default:0}
  }
}
